using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Xml;

namespace My.IdlConverter.LoadManager.Cfg
{
    public class SearchPatterConfig : ConfigurationSection
    {
        private string _rootPath="";
        private string _scope ="";
        private string _pattern = "";
        private string _strategy = "";
        private string _componentLoader = "";
        private string _typeName = "";
        private bool _recursive = true;

        public SearchPatterConfig()
        {
        }

        public string RootPath
        {
            get { return _rootPath; }
            set { _rootPath = value; }
        }

        public string Scope
        {
            get { return _scope; }
            set { _scope = value; }
        }

        public string Pattern
        {
            get { return _pattern; }
            set { _pattern = value; }
        }

        public string Strategy
        {
            get { return _strategy; }
            set { _strategy = value; }
        }

        public string ComponentLoader
        {
            get { return _componentLoader; }
            set { _componentLoader = value; }
        }

        public bool Recursive
        {
            get { return _recursive; }
            set { _recursive = value; }
        }

        public string TypeName
        {
            get { return _typeName; }
            set { _typeName = value; }
        }

        protected override void DeserializeSection(XmlReader reader)
        {
            while(reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.Name == "RootPath")
                        RootPath = reader.ReadString();
                    else if (reader.Name == "Scope")
                        Scope = reader.ReadString();
                    else if (reader.Name == "Pattern")
                        Pattern = reader.ReadString();
                    else if (reader.Name == "Strategy")
                        Strategy = reader.ReadString();
                    else if (reader.Name == "ComponentLoader")
                        ComponentLoader = reader.ReadString();
                    else if (reader.Name == "Recursive")
                        Recursive = Convert.ToBoolean(reader.ReadString());
                    else if (reader.Name == "TypeName")
                        TypeName = reader.ReadString();
                }
            }
        }

        protected override string SerializeSection(ConfigurationElement parentElement, string name, ConfigurationSaveMode saveMode)
        {
            XmlDocument document = new XmlDocument();

            XmlNode rootPathNode = document.CreateElement("RootPath");
            rootPathNode.AppendChild(document.CreateTextNode(RootPath));
            XmlNode scopeNode = document.CreateElement("Scope");
            scopeNode.AppendChild(document.CreateTextNode(Scope));
            XmlNode patternNode = document.CreateElement("Pattern");
            patternNode.AppendChild(document.CreateTextNode(Pattern));
            XmlNode strategyNode = document.CreateElement("Strategy");
            strategyNode.AppendChild(document.CreateTextNode(Strategy));
            XmlNode componentLoaderNode = document.CreateElement("ComponentLoader");
            componentLoaderNode.AppendChild(document.CreateTextNode(ComponentLoader));
            XmlNode recursiveNode = document.CreateElement("Recursive");
            recursiveNode.AppendChild(document.CreateTextNode(Recursive.ToString()));
            XmlNode typeNameNode = document.CreateElement("TypeName");
            typeNameNode.AppendChild(document.CreateTextNode(TypeName));

            XmlNode parentNode = document.CreateElement(name);

            parentNode.AppendChild(rootPathNode);
            parentNode.AppendChild(scopeNode);
            parentNode.AppendChild(patternNode);
            parentNode.AppendChild(strategyNode);
            parentNode.AppendChild(componentLoaderNode);
            parentNode.AppendChild(recursiveNode);
            parentNode.AppendChild(typeNameNode);

            document.AppendChild(parentNode);

            return document.InnerXml;
        }
    }
}
